package com.yanchangapp.ui.activity._n_manage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yanchangapp.R;
import com.yanchangapp.adapter._n_manage.AttachmentAdapter;
import com.yanchangapp.adapter._n_manage.OperateBookAdapter;
import com.yanchangapp.commom.LoadingType;
import com.yanchangapp.model.bean.AttachBean;
import com.yanchangapp.model.bean.OperateBookBean;
import com.yanchangapp.model.response.OperateBookResponse;
import com.yanchangapp.model.response.PlanDetailResponse;
import com.yanchangapp.ui.activity.mail.AttachmentShowActivity;
import com.yanchangapp.ui.presenter.OperateBookAtPresenter;
import com.yanchangapp.ui.view.OperateBookAtView;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.ui.ResHelper;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.widget.NiceNestedScrollView;
import com.zxl.baselib.widget.YCLoadingView;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
/**
 * page: 作业指导书...
 *
 * 未开始状态  【点击开始工作】 -> 任务指导书页面
 *
 */
public class OperateBookActivity extends BaseActivity<OperateBookAtView, OperateBookAtPresenter> implements OperateBookAtView {

    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.btnStartWork)
    Button btnStartWork;
    @BindView(R.id.bottom)
    LinearLayout bottom;
    @BindView(R.id.nestScrollview)
    NiceNestedScrollView nestScrollview;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.ycLoading)
    YCLoadingView ycLoading;
    @BindView(R.id.rvOperateBook)
    RecyclerView rvOperateBook;
    @BindView(R.id.llAttach)
    LinearLayout llAttach;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.tvReadAttach)
    TextView tvReadAttach;
    @BindView(R.id.rvAttachmentLists)
    RecyclerView rvAttachmentLists;
    @BindView(R.id.tvTag)
    TextView tvTag;
    @BindView(R.id.tvTime)
    TextView tvTime;

    private static final String PLAN_ID = "plan_id";
    // 计划ID....
    private long planID;
    private OperateBookAdapter mOperateBookAdapter;
    private List<OperateBookBean> mOperateBookBeans = new ArrayList<>();
    private AttachmentAdapter mAttachmentAdapter;
    private List<AttachBean> mAttachBeans = new ArrayList<>();
    private boolean isSelected = false;

    private void setLoadingState(LoadingType loadingType){
        switch (loadingType){
            case LOADING:
                if (ycLoading.getVisibility() == View.GONE){
                    ycLoading.setVisibility(View.VISIBLE);
                }
                if (llErrorNetwork.getVisibility() == View.VISIBLE){
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (nestScrollview.getVisibility() == View.VISIBLE){
                    nestScrollview.setVisibility(View.GONE);
                    bottom.setVisibility(View.GONE);
                }
                break;
            case SUCCESS:
                if (ycLoading.getVisibility() == View.VISIBLE){
                    ycLoading.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE){
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (nestScrollview.getVisibility() == View.GONE){
                    nestScrollview.setVisibility(View.VISIBLE);
                    bottom.setVisibility(View.VISIBLE);
                }
                break;
            case ERROR:
                if (ycLoading.getVisibility() == View.VISIBLE){
                    ycLoading.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.GONE){
                    llErrorNetwork.setVisibility(View.VISIBLE);
                }

                // 设置布局隐藏
                if (nestScrollview.getVisibility() == View.VISIBLE){
                    nestScrollview.setVisibility(View.GONE);
                    bottom.setVisibility(View.GONE);
                }
                break;
        }
    }

    /**
     * 开始工作点击事件...........
     *
     * @param activity
     * @param planID
     */
    public static void show(Activity activity,long planID) {
        Intent intent = new Intent(activity, OperateBookActivity.class);
        intent.putExtra(PLAN_ID, planID);
        activity.startActivity(intent);
    }

    @Override
    protected void init() {
        planID =  getIntent().getLongExtra(PLAN_ID,-1);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_operate_book;
    }

    @Override
    protected OperateBookAtPresenter createPresenter() {
        return new OperateBookAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("作业指导书");
        initAdapter();
    }

    private void initAdapter() {
        LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        manager.setSmoothScrollbarEnabled(true);
        manager.setAutoMeasureEnabled(true);
        rvOperateBook.setLayoutManager(manager);
        rvOperateBook.setHasFixedSize(true);
        rvOperateBook.setNestedScrollingEnabled(false);
        mOperateBookAdapter = new OperateBookAdapter(mOperateBookBeans);
        rvOperateBook.setAdapter(mOperateBookAdapter);

        // 设置附件的adapter类
        LinearLayoutManager attachmentManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        attachmentManager.setSmoothScrollbarEnabled(true);
        attachmentManager.setAutoMeasureEnabled(true);
        rvAttachmentLists.setLayoutManager(attachmentManager);
        mAttachmentAdapter = new AttachmentAdapter(mAttachBeans);
        rvAttachmentLists.setAdapter(mAttachmentAdapter);
    }

    @Override
    protected void initData() {
        setLoadingState(LoadingType.LOADING);
        mPresenter.queryPlanDetail(planID);
    }

    @Override
    protected void initListener() {
        ivToolbarNavigation.setOnClickListener(v -> finish());
        /*
         * 开始工作点击事件....
         */
        btnStartWork.setOnClickListener(v -> {
            if (!isSelected){
                UIUtils.showToast("我已认真阅读，将按作业指导书操作");
            }else {
                mPresenter.startPlan(planID);
            }
        });

        mAttachmentAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.tvLook){
                AttachmentShowActivity.show(OperateBookActivity.this,mAttachBeans.get(position).getAttachmentUrl());
            }
        });


        checkbox.setOnClickListener(v -> isSelected = !isSelected);

        tvReadAttach.setOnClickListener(v -> {
            isSelected = !isSelected;
            checkbox.setChecked(isSelected);
        });

    }

    @Override
    public void getOperateDetailSuccess(PlanDetailResponse response) {
        setLoadingState(LoadingType.SUCCESS);
        tvTag.setText(response.getScheduleStatusName());
        tvTag.setBackground(ResHelper.getDrawable(R.drawable.tag_order_default));
        // 进行任务指导书内容数据刷新...
        mOperateBookBeans.addAll(response.getOperationList());
        mOperateBookAdapter.notifyDataSetChanged();

        if ((response.getAttachments() != null)
                && (response.getAttachments().size() != 0)){
            mAttachBeans.addAll(response.getAttachments());
            mAttachmentAdapter.notifyDataSetChanged();
        }else {
            llAttach.setVisibility(View.GONE);
        }
        // 设置标题
        tvToolbarTitle.setText(response.getTitle());
        if (response.getStandardTime() == null){
            tvTime.setText("暂无");
        }else {
            tvTime.setText(getString(R.string.strand_time,response.getStandardTime(),response.getStandardTimeTypeName()));
        }
    }

    @Override
    public void getOperateDetailFailure(String err) {
        setLoadingState(LoadingType.ERROR);
        UIUtils.showToast(err);
    }

    @Override
    public void startPlanSuccess(String success) {
        UIUtils.showToast(success);
        // 发送开始工作成功通知
        mRxManager.post(DailyPlanActivity.CMD_START_WORK,true);
        finish();
        // 跳转向进行中页面
        LoadingOperateBookActivity.show(OperateBookActivity.this,planID);
    }

    @Override
    public void startPlanFailure(String err) {
        UIUtils.showToast(err);
    }
}
